/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: CommonHandler
 * @Package com.rx.core.common
 * @Description: 公共处理器
 * @author: 陈锦韬
 * @date: 2021\6\15 0015
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rx.core.anno.RxAlias;
import com.rx.core.base.RxBaseAlias;
import com.rx.core.base.RxBaseData;
import com.rx.core.dbase.RxBaseMapper;
import com.rx.core.dbase.RxMarkDbMust;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author: Administrator
 * @Description: 公共处理器.一些公共的处理逻辑，可直接配置使用无须自定义
 * @date: 2021\6\15 0015
 */
@RxHandler
@RxAlias(RxBaseAlias.USE_MYBATIS_XML)
@Slf4j
public class CommonHandler implements RxHandlerContent {
    @Override
    public boolean handle(RxBaseData data) {
        data.setUseMybatisXml(true);
        return false;
    }

    /**
     * 删除失败，不会抛出异常
     * @param data
     * @return
     */
    @RxHandler(types= {HANDLER_TYPE.SINGLE_DELETE})
    @RxAlias(RxBaseAlias.NO_THROW_DELETE)
    public boolean noThrowDelete(RxBaseData data){
        RxBaseMapper mapper = data.databaseMapper();
        if(data.isUseMybatisXml()){
            data.setResultData(mapper.delete(data));
        }else {
            QueryWrapper queryWrapper = data.queryWrapper(false);
            data.setResultData(mapper.delete(queryWrapper));
        }
        return false;
    }

    /**
     * 新增失败不会抛出异常
     * @param data
     * @return
     */
    @RxHandler(types= {HANDLER_TYPE.SINGLE_INSERT})
    @RxAlias(RxBaseAlias.NO_THROW_INSERT)
    public boolean noThrowInsert(RxBaseData data){
        // 获取Dao
        RxBaseMapper mapper = data.databaseMapper();
        Integer integer = 0;
        // 赋值基本用户信息
        data.assign(true);
        if(data.isUseMybatisXml()){
            integer = mapper.append(data);
        }else {
            integer = mapper.insert(data);
        }
        data.setResultData(integer);
        return false;
    }

    /**
     * 更新失败不会抛出异常
     * @param data
     * @return
     */
    @RxHandler(types= {HANDLER_TYPE.SINGLE_UPDATE})
    @RxAlias(RxBaseAlias.NO_THROW_UPDATE)
    public boolean noThrowUpdate(RxBaseData data){
        // 获取Dao
        RxBaseMapper mapper = data.databaseMapper();
        // 赋值基本用户信息
        data.assign(false);
        QueryWrapper queryWrapper = data.queryWrapper(true);
        if(data.isUseMybatisXml()){
            data.setResultData(mapper.update(data));
        }else {
            data.setResultData(mapper.update(data,queryWrapper));
        }

        return false;
    }

    static List getPageList(Object object){
        if (object instanceof Page){
            if (EmptyChecker.isEmpty(((Page) object).getRecords())){
                return Collections.emptyList();
            }
            return ((Page) object).getRecords();
        }
        if(object instanceof List){
            return (List)object;
        }
        return Collections.emptyList();
    }

    public static void removeFilter(Object object,List<String> fieldList){
        if (object instanceof RxBaseData){
            ((RxBaseData)object).removeFilter(fieldList);
        }
    }

    public static void showFields(RxBaseData data,List<String> fieldList){


        Object object = data.getResultData();
        for (Object o : getPageList(object)) {
            removeFilter(o,fieldList);
        }
        removeFilter(object,fieldList);
        data.removeFilter(fieldList);
    }


    /**
     *返回前端显示更新信息
     * @param data
     * @return
     */
    @RxHandlerAfter(types= {HANDLER_TYPE.SINGLE_PAGE})
    @RxAlias(RxBaseAlias.SHOW_CREATE_FIELD)
    public boolean showCreateField(RxBaseData data){

        List<String> fieldList = Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_BY_ID),
                RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_DATE));

        showFields(data,fieldList);
        return false;
    }



    /**
     * 返回前端显示更新信息
     * @param data
     * @return
     */
    @RxHandlerAfter(types= {HANDLER_TYPE.SINGLE_PAGE})
    @RxAlias(RxBaseAlias.SHOW_UPDATE_FIELD)
    public boolean showUpdateField(RxBaseData data){

        List<String> fieldList =Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.UPDATE_BY_ID),
                RxUtil.StringUtil.lineToHump(RxMarkDbMust.UPDATE_DATE));
        showFields(data,fieldList);
        return false;
    }

    /**
     *
     * @param data
     * @return
     */
    @RxHandlerPrev(types= {HANDLER_TYPE.SINGLE_PAGE})
    @RxAlias(RxBaseAlias.SELECT_CREATE_FIELD)
    public boolean selectCreateField(RxBaseData data){

        List<String> fieldList = Arrays.asList(RxMarkDbMust.CREATE_BY_ID.toUpperCase(),RxMarkDbMust.CREATE_DATE.toUpperCase());
        data.removeSqlFilter(fieldList);

        return false;
    }

    /**
     *
     * @param data
     * @return
     */
    @RxHandlerPrev(types= {HANDLER_TYPE.SINGLE_PAGE})
    @RxAlias(RxBaseAlias.SELECT_UPDATE_FIELD)
    public boolean selectUpdateField(RxBaseData data){

        List<String> fieldList =Arrays.asList(RxMarkDbMust.UPDATE_BY_ID.toUpperCase(),RxMarkDbMust.UPDATE_DATE.toUpperCase());
        data.removeSqlFilter(fieldList);
        return false;
    }
}
